<template>
  <div class="table-page">
    <GiTable row-key="id" title="微信名片访问记录管理" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.openId" placeholder="请输入访客昵称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <!-- <a-button v-permission="['card:businessCardVisitorRecord:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button> -->
        <!-- <a-button v-permission="['card:businessCardVisitorRecord:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button> -->
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['card:businessCardVisitorRecord:list']" title="查看" @click="onDetail(record)">查看</a-link>
        </a-space>
      </template>
      <template #cardName="{ record }">
        <a-link @click="onCardDetail(record)">
          <a-typography-paragraph class="link-text" :ellipsis="{
            rows: 1,
            showTooltip: true,
            css: true,
          }">
            {{ record.cardName }}
          </a-typography-paragraph>
        </a-link>
      </template>
      <template #visitorName="{ record }">
        <GiCellAvatar :avatar="record.avatar" :name="record.nickname" />
      </template>
    </GiTable>

    <BusinessCardVisitorRecordAddModal ref="BusinessCardVisitorRecordAddModalRef" @save-success="search" />
    <BusinessCardVisitorRecordDetailDrawer ref="BusinessCardVisitorRecordDetailDrawerRef" />
    <BusinessCardDetailDrawer ref="BusinessCardDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import { exportBusinessCardVisitorRecord, listBusinessCardVisitorRecord, type BusinessCardVisitorRecordQuery, type BusinessCardVisitorRecordResp } from '@/apis/website'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import BusinessCardDetailDrawer from '../card/BusinessCardDetailDrawer.vue'
import BusinessCardVisitorRecordAddModal from './BusinessCardVisitorRecordAddModal.vue'
import BusinessCardVisitorRecordDetailDrawer from './BusinessCardVisitorRecordDetailDrawer.vue'

defineOptions({ name: 'BusinessCardVisitorRecord' })


const queryForm = reactive<BusinessCardVisitorRecordQuery>({
  openId: undefined,
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listBusinessCardVisitorRecord({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    fixed: !isMobile() ? 'left' : undefined,
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '微信名片', dataIndex: 'cardName', slotName: 'cardName' },
  { title: '访客', dataIndex: 'visitorName', slotName: 'visitorName' },
  { title: '访问时间', dataIndex: 'visitorTime', slotName: 'visitorTime' },
  { title: '停留时间（秒）', dataIndex: 'duration', slotName: 'duration' },
  { title: '访问者IP地址', dataIndex: 'ipAddress', slotName: 'ipAddress' },
  { title: '设备类型', dataIndex: 'deviceType', slotName: 'deviceType' },
  { title: '操作系统', dataIndex: 'operatingSystem', slotName: 'operatingSystem' },
  { title: '操作类型', dataIndex: 'actions', slotName: 'actions' },
  // { title: '描述', dataIndex: 'description', slotName: 'description' },
  // { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  // { title: '创建人', dataIndex: 'createUser', slotName: 'createUser' },
  // { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  // { title: '修改人', dataIndex: 'updateUser', slotName: 'updateUser' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['card:businessCardVisitorRecord:update', 'card:businessCardVisitorRecord:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.openId = undefined
  search()
}


// 导出
const onExport = () => {
  useDownload(() => exportBusinessCardVisitorRecord(queryForm))
}

const BusinessCardVisitorRecordAddModalRef = ref<InstanceType<typeof BusinessCardVisitorRecordAddModal>>()
// 新增
const onAdd = () => {
  BusinessCardVisitorRecordAddModalRef.value?.onAdd()
}

const BusinessCardVisitorRecordDetailDrawerRef = ref<InstanceType<typeof BusinessCardVisitorRecordDetailDrawer>>()
// 详情
const onDetail = (record: BusinessCardVisitorRecordResp) => {
  BusinessCardVisitorRecordDetailDrawerRef.value?.onDetail(record.id)
}

const BusinessCardDetailDrawerRef = ref<InstanceType<typeof BusinessCardDetailDrawer>>()
//查看名片详情
const onCardDetail = (record: BusinessCardVisitorRecordResp) => {
  console.log('ok')
  BusinessCardDetailDrawerRef.value?.onDetail(record.cardId)
}
</script>

<style lang="scss" scoped></style>
